package com.pybox.common.utils;

/**
 * Created by wuyingkai on 2018/9/20.
 */


import com.pybox.dto.TCustomersDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JdbcTemplateUtils<T> {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplateUtils() {
    }

    public String query(String querySql, int model_id){
        try {
           return this.jdbcTemplate.queryForObject(querySql,String.class,model_id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }


    public Page<T> page(String querySql, List<Object> paramList, Class<T> mappedClass, int pageNo, int pageSize) {
        JdbcTemplate var10000 = this.jdbcTemplate;
        BeanPropertyRowMapper var10003 = new BeanPropertyRowMapper(mappedClass);
        --pageNo;
        return JdbcTemplatePagination.queryPage(var10000, querySql, paramList, var10003, pageNo, pageSize);
    }

    public Page<T> page(StringBuffer querySql, List<Object> paramList, Class<T> mappedClass, int pageNo, int pageSize) {
        return this.page(querySql.toString(), paramList, mappedClass, pageNo, pageSize);
    }

    public Page<T> page(QuerySqlUtils querySqlUtils, Class<T> mappedClass, int pageNo, int pageSize) {
        return this.page(querySqlUtils.getQuerySql(), querySqlUtils.getParamList(), mappedClass, pageNo, pageSize);
    }

    public List<T> list(String querySql, List<Object> paramList, Class<T> mappedClass) {
        return this.jdbcTemplate.query(querySql, paramList.toArray(), new BeanPropertyRowMapper(mappedClass));
    }

    public List<T> list(StringBuffer querySql, List<Object> paramList, Class<T> mappedClass) {
        return this.list(querySql.toString(), paramList, mappedClass);
    }

    public List<T> list(QuerySqlUtils querySqlUtils, Class<T> mappedClass) {
        return this.list(querySqlUtils.getQuerySql(), querySqlUtils.getParamList(), mappedClass);
    }

    public long update(StringBuffer updateSql, List<Object> paramList) {
        return (long)this.jdbcTemplate.update(updateSql.toString(), paramList.toArray());
    }

    public long update(String updateSql, List<Object> paramList) {
        return (long)this.jdbcTemplate.update(updateSql, paramList.toArray());
    }

    public <K> K queryForObject(StringBuffer querySql, List<Object> paramList, Class<K> requiredType) {
        try {
            return this.jdbcTemplate.queryForObject(querySql.toString(), paramList.toArray(), requiredType);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }

    public <K> K queryForObject(String sql,Class<K> requiredType) {
        try {
            return this.jdbcTemplate.queryForObject(sql.toString(), requiredType);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }

    public <K> K queryForObject(String querySql, List<Object> paramList, Class<K> requiredType) {
        try {
            return this.jdbcTemplate.queryForObject(querySql, paramList.toArray(), requiredType);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }

    public <K> K queryForObject(StringBuffer querySql, List<Object> paramList, RowMapper<K> requiredType) {
        try {
            return this.jdbcTemplate.queryForObject(querySql.toString(), paramList.toArray(), requiredType);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }

    public <K> K queryForObject(String querySql, List<Object> paramList, RowMapper<K> requiredType) {
        try {
            return this.jdbcTemplate.queryForObject(querySql, paramList.toArray(), requiredType);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }

    public Object queryForObject(String sql, BeanPropertyRowMapper<TCustomersDto> tCustomersDtoBeanPropertyRowMapper, int customerId) {
        try {
            return this.jdbcTemplate.queryForObject(sql, tCustomersDtoBeanPropertyRowMapper,customerId);
        } catch (EmptyResultDataAccessException var5) {
            return null;
        } catch (IncorrectResultSizeDataAccessException var6) {
            throw new RuntimeException("查询结果大于1条");
        }
    }
}
